"""
读取json文件

Version: 0.1
Author: yuwl
Date：2024-4-07
"""

import json


def main():
    mydict = {
        'name': '张三',
        'age': 38,
        'qq': 957658,
        'friends': ['李斯', '王伍'],
        'cars':[
            {'brand': 'BYD', 'max_speed': 180},
            {'brand': 'Audi', 'max_speed': 280},
            {'brand': 'Benz', 'max_speed': 320}
        ]
    }
    try:
        with open('data.json', 'w', encoding='utf-8') as f:
            json.dump(mydict, f)
    except IOError as e:
        print(f'写文件错误：{e}')
    print('保存数据完成！')


    teacher_dict = {'name': '李元芳', 'age': 25, 'title': '讲师'}
    json_str = json.dumps(teacher_dict)
    print(json_str)
    print(type(json_str))

    fruits_list = ['apple', 'orange', 'strawberry', 'banana', 'pitaya']
    json_str = json.dumps(fruits_list)
    print(json_str)
    print(type(json_str))


if __name__ == '__main__':
    main()